package com.quest.space.handler;

import com.quest.space.MainWindow;
import com.quest.space.data.enums.EventEnum;
import com.quest.space.data.MainContext;
import com.quest.space.data.UserCache;

import javax.swing.*;

import static com.quest.space.data.DataCenter.functionList;

public class UserHandler {
    public static String name = "user";
    public static String password = "123456";

    public static void login() {
        MainContext context = MainWindow.mainContext;
        JTextArea textField2 = context.getTextArea4();
        JTextArea sysArea = context.getSysArea();
        JTextArea textArea3 = context.getTextArea3();
        //调用登录接口，返回系统功能列表
        if (name.equals(textArea3.getText()) && password.equals(textField2.getText())) {
            UserCache.eventEnum = EventEnum.MAIN_MENU;
            UserCache.token = "1234444";
            UserCache.userId = "111";
            showMenu();
        }else {
            sysArea.setText("账号密码错误，请重新输入");
        }
        textArea3.setText(null);
        textField2.setText(null);
    }

    public static void showMenu() {
        MainContext context = MainWindow.mainContext;
        JTextArea textField2 = context.getTextArea4();
        JLabel userLabe1 = context.getUserLabe1();
        JLabel userLabe2 = context.getUserLabe2();
        JTextArea sysArea = context.getSysArea();
        JTextArea textArea4 = context.getTextArea4();
        UserCache.eventEnum = EventEnum.MAIN_MENU;
        sysArea.setText("you can choose:\n" + functionList);
        userLabe1.setText("user");
        userLabe2.setVisible(false);
        textField2.setVisible(false);
        textArea4.setText("");
        context.getButton2().setVisible(true);
        context.getButton3().setVisible(false);
        context.getTextArea1().setText("");
        context.getTextArea2().setText("");
        context.getTextArea3().setText("");
        context.getTextArea4().setText("");

        clear();
    }

    private static void clear() {
        UserCache.table = null;
        UserCache.gameId = null;
    }
}
